import { http } from 'utils'

export async function getArticles(payload) {
  try {
    const res = await http.get('articles/list', payload)
    if (res.code === 1) {
      const data = res.data
      return data
    }
    throw new Error(res.info)
  } catch (err) {
    throw new Error(err)
  }
}

export async function saveArticles(payload) {
  const path = payload.id ? 'articles/update' : 'articles/save'
  try {
    const res = await http.post(path, payload)
    if (res.code === 1) {
      return res
    }
    throw new Error(res.data)
  } catch (err) {
    throw new Error(err)
  }
}

export async function getArticlesGroup() {
  try {
    const res = await http.get('articles/groupList')
    if (res.code === 1) {
      return res.data
    }
    throw new Error(res.data)
  } catch (err) {
    throw new Error(err)
  }
}

export async function deleteGroup(id) {
  try {
    const res = await http.get('articles/deleteGroup', { id })
    if (res.code === 1) {
      return res
    }
    throw new Error(res.data)
  } catch (err) {
    throw new Error(err)
  }
}

export async function addArticlesGroup(name) {
  try {
    const res = await http.post('articles/addGroup', { name })
    if (res.code === 1) {
      return res
    }
    throw new Error(res.data)
  } catch (err) {
    throw new Error(err)
  }
}

export async function delArticle(id) {
  try {
    const res = await http.get('articles/delete', { id })
    if (res.code === 1) {
      return res
    }
    throw new Error(res.data)
  } catch (err) {
    throw new Error(err)
  }
}

export async function getArticleInfo(id) {
  try {
    const res = await http.get('articles/info', { id })
    if (res.code === 1) {
      return res.data
    }
    throw new Error(res.data)
  } catch (err) {
    throw new Error(err)
  }
}
